package com.ssyt.yt1.day30.echo.demo3;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class EchoThread implements Runnable {

	private Socket socket;

	public EchoThread(Socket socket) {
		super();
		this.socket = socket;
	}

	@Override
	public void run() {
		try {
			// 2）获得输入输出流、3）读写数据
			Scanner scanner = null;
			OutputStream outputStream = null;
			InputStream inputStream = null;

			inputStream = socket.getInputStream();
			scanner = new Scanner(inputStream);
			String result = scanner.nextLine();

			outputStream = socket.getOutputStream();
			outputStream.write(("Echo:" + result + "\n").getBytes());
			// 4）关闭输入输出流
			outputStream.close();
			scanner.close();
			inputStream.close();
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
